package com.toman.Adpter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.toman.R;

import com.toman.bean.ItemTypeBean;

import java.util.List;

/**
 * Created by 邹苏隆 on 2018/5/10.
 * description：
 */

public class ItemTypeCheckOldAdapter extends AdapterBase<ItemTypeBean, ItemTypeCheckOldHolder> implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private boolean editable = true;

    public interface ActionClickListener {
        public void onDetailClick(ItemTypeBean bean);

        //查看详情
        public void onCheckClick(ItemTypeBean bean);
//审核
    }

    private LayoutInflater inflater;
    private Context mContext;
    private ActionClickListener actionClickListener;

    public void setActionClickListener(ActionClickListener l) {
        actionClickListener = l;
    }

    public ItemTypeCheckOldAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        this.mContext = context;

    }


    @Override
    protected void handlerData(final List<ItemTypeBean> mList2, final int position, final ItemTypeCheckOldHolder itemView) {
        ItemTypeBean bean = mList2.get(position);
        itemView.tv_name.setText(bean.getName());
        itemView.tv_more.setOnClickListener(this);

        itemView.tv_more.setTag(position);
//        不先清空监听器的话，按钮显示会出问题
        itemView.sw_status.setOnCheckedChangeListener(null);

        if (bean.getCheck_status().equals("0")) {
            itemView.sw_status.setChecked(true);
            itemView.tv_status.setText("通过");
            itemView.tv_status.setTextColor(Color.parseColor("#53D666"));

        } else {
            itemView.sw_status.setChecked(false);
            itemView.tv_status.setText("不通过");
            itemView.tv_status.setTextColor(Color.parseColor("#CCCCCE"));
        }
        itemView.sw_status.setTag(position);

        itemView.sw_status.setOnCheckedChangeListener(this);

    }

    @Override
    protected ItemTypeCheckOldHolder getItemViewHolder() {
        return new ItemTypeCheckOldHolder();
    }

    @Override
    protected View initConvertView(View convertView, ItemTypeCheckOldHolder v) {
        convertView = inflater.inflate(R.layout.item_item_check_old, null);
        v.tv_name = (TextView) convertView
                .findViewById(R.id.tv_name);
        v.tv_status = (TextView) convertView
                .findViewById(R.id.tv_status);
        v.tv_more = (TextView) convertView
                .findViewById(R.id.tv_more);
        v.sw_status = (Switch) convertView.findViewById(R.id.sw_status);
        return convertView;
    }

    @Override
    protected void onReachBottom() {

    }

    @Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        ItemTypeBean bean = getItem(position);

        switch (view.getId()) {
            case R.id.tv_more:
                if (actionClickListener != null) {
                    actionClickListener.onDetailClick(bean);
                }
                break;


        }

    }

    @Override
    public void onCheckedChanged(CompoundButton view, boolean b) {

        if (editable) {
            int position = (Integer) view.getTag();
            ItemTypeBean bean = getItem(position);
            if (b) {
                bean.setCheck_status("0");
                notifyDataSetChanged();
            } else {
                bean.setCheck_status("1");
                notifyDataSetChanged();
            }
        } else {
            view.setChecked(!b);
        }
    }

    public void isEdit(boolean b) {
        editable = b;
    }
}


class ItemTypeCheckOldHolder {
    TextView tv_name;
    TextView tv_more;
    TextView tv_status;
    Switch sw_status;
}
